﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace BusinessLogicLayer
{
    public class OnSiteManager
    {

        public ActionResult ThemOnSite(OnSite os, List<NhanVien> listNV)
        {
            ActionResult ar = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var listos = db.OnSites.ToList();
                    if (listos.Exists(o => o.MaOnSite == os.MaOnSite))
                    {
                        ar.AddException(new Exception("Ma OnSite : " + os.MaOnSite + " đã tồn tại trong danh sách"));
                        return ar;

                    }
                    else
                    {
                        foreach (var item in listNV)
                        {
                            var nv = db.NhanViens.Single(n => n.MaNhanVien == item.MaNhanVien);
                            os.NhanViens.Add(nv);

                        }
                        db.OnSites.Add(os);
                        db.SaveChanges();
                        os = db.OnSites.Include("NhanViens").Single(n => n.MaOnSite == os.MaOnSite);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;

        }

        public ActionResult XoaOnSite(string maonsite, List<NhanVien> listNV)
        {

            ActionResult ar = new ActionResult();
            using (var db = new QLNVDbContext())
            {
                try
                {

                    var os = db.OnSites.Single(s => s.MaOnSite == maonsite);
                    foreach (var item in listNV)
                    {
                        var nv = db.NhanViens.Single(n => n.MaNhanVien == item.MaNhanVien);
                        os.NhanViens.Remove(item);
                    }
                    db.OnSites.Remove(os);

                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }
                return ar;
            }
        }
        public ActionResult XoaOnSiteNV(string maonsite, string maNV)
        {

            ActionResult ar = new ActionResult();
            using (var db = new QLNVDbContext())
            {
                try
                {

                    var os = db.OnSites.Single(s => s.MaOnSite == maonsite);
                    var nv = db.NhanViens.Single(n => n.MaNhanVien == maNV);
                    os.NhanViens.Remove(nv);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }
                return ar;
            }
        }
        public ActionResult SuaOnSite(OnSite onsite, List<NhanVien> listNV)
        {
            ActionResult ar = new ActionResult();

            try
            {
                using (var db = new QLNVDbContext())
                {
                    OnSite os = db.OnSites.Single(o => o.MaOnSite == onsite.MaOnSite);
                    os.NhanViens.Clear();
                    os.MaOnSite = onsite.MaOnSite;
                    os.DiaDiem = onsite.DiaDiem;
                    os.CongTy = onsite.CongTy;
                    os.NgayBD = onsite.NgayBD;
                    os.NgayKT = onsite.NgayKT;

                    foreach (var item in listNV)
                    {
                        var nv = db.NhanViens.Single(n => n.MaNhanVien == item.MaNhanVien);
                        os.NhanViens.Add(nv);

                    }
                    db.SaveChanges();
                }

            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResultAsList<OnSite> LayDSOnSite()
        {
            ActionResultAsList<OnSite> ob = new ActionResultAsList<OnSite>();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    ob.ObjectList = db.OnSites.ToList();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ob.AddException(ex);
                }

            }
            return ob;
        }
        public ActionResultAsList<NhanVien> LayDSNhanVienTheoMaOnSite(string maos)
        {
            ActionResultAsList<NhanVien> ob = new ActionResultAsList<NhanVien>();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    var list = db.OnSites.Include("NhanViens").Where(d => d.MaOnSite == maos).ToList();
                    ob.ObjectList = list.First().NhanViens.ToList();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ob.AddException(ex);
                }

            }
            return ob;
        }
        public ActionResultAsList<KyNang> LayDSKyNangTheoMaNV(string manv)
        {
            ActionResultAsList<KyNang> ob = new ActionResultAsList<KyNang>();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    var list = db.NhanViens.Include("KyNangs").Where(d => d.MaNhanVien == manv).ToList();
                    ob.ObjectList = list.First().KyNangs.ToList();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ob.AddException(ex);
                }

            }
            return ob;
        }

        public ActionResultAsList<OnSite> TimKiemOnSiteTheoMa(string maonsite)
        {
            ActionResultAsList<OnSite> ob = new ActionResultAsList<OnSite>();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    ob.ObjectList = db.OnSites.Where(o => o.MaOnSite == maonsite).ToList();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ob.AddException(ex);
                }

            }
            return ob;
        }
        public ActionResultAsList<OnSite> TimKiemOnSiteTheoCongTy(string congty)
        {
            ActionResultAsList<OnSite> ob = new ActionResultAsList<OnSite>();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    ob.ObjectList = db.OnSites.Where(o => o.CongTy.Contains(congty)).ToList();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ob.AddException(ex);
                }

            }
            return ob;
        }
        public ActionResultAsList<OnSite> TimKiemOnSiteTheoNgayBD(DateTime ngay)
        {
            ActionResultAsList<OnSite> ob = new ActionResultAsList<OnSite>();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    ob.ObjectList = db.OnSites.Where(o => o.NgayBD.CompareTo(ngay) == 0).ToList();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ob.AddException(ex);
                }

            }
            return ob;
        }
        public ActionResultAsList<OnSite> TimKiemOnSiteTheoDiaDiem(string diadiem)
        {
            ActionResultAsList<OnSite> ob = new ActionResultAsList<OnSite>();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    ob.ObjectList = db.OnSites.Where(o => o.DiaDiem.Contains(diadiem)).ToList();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ob.AddException(ex);
                }

            }
            return ob;
        }
        public ActionResult DoiTrangThaiThanhOnSite(List<string> manv)
        {
            ActionResult ac = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {

                    foreach (string ma in manv)
                    {
                        NhanVien nhanvien = db.NhanViens.Single(n => n.MaNhanVien == ma);
                        nhanvien.TrangThai = "On-Site";
                    }

                    db.SaveChanges();

                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ac.AddException(ex);
            }
            return ac;
        }
        public ActionResult DoiTTNVKhiXoa(List<NhanVien> LNV)
        {
            DateTime dt = DateTime.Today;
            ActionResult ac = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    foreach (var item in LNV)
                    {
                        NhanVien nv = db.NhanViens.Single(n => n.MaNhanVien == item.MaNhanVien);
                        var da = db.DuAns.Include("NhanViens").Where(d => d.NhanViens.Where(n => n.MaNhanVien == item.MaNhanVien).FirstOrDefault<NhanVien>() != null).ToList();
                        if (!da.Any())
                        {
                            nv.TrangThai = TrangThaiNV.RanhRoi;

                        }
                        else
                        {
                            var ss = db.DuAns.Where(d => d.NgayBD.CompareTo(dt) < 0 && d.NgayKT.CompareTo(dt) > 0);
                            if (ss != null)
                                nv.TrangThai = TrangThaiNV.LamViec;
                        }

                    }
                    db.SaveChanges();

                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ac.AddException(ex);
            }
            return ac;
        }
        public ActionResultAsList<NhanVien> LayDSNVTheoTrangThai(string trangthai)
        {
            ActionResultAsList<NhanVien> ob = new ActionResultAsList<NhanVien>();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    ob.ObjectList = db.NhanViens.Where(o => o.TrangThai == trangthai).ToList();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ob.AddException(ex);
                }

            }
            return ob;
        }
        public ActionResultAsList<NhanVien> TimNhanVienTheoMa(string manv)
        {
            ActionResultAsList<NhanVien> ob = new ActionResultAsList<NhanVien>();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    ob.ObjectList = db.NhanViens.Where(o => o.MaNhanVien == manv).ToList();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ob.AddException(ex);
                }

            }
            return ob;
        }
        public List<NhanVien> LayDSNhanVienTheoList(List<string> listmanv)
        {

            List<NhanVien> list = new List<NhanVien>();
            using (var db = new QLNVDbContext())
            {

                foreach (string ma in listmanv)
                {
                    NhanVien nhanvien = db.NhanViens.Single(n => n.MaNhanVien == ma);
                    list.Add(nhanvien);
                }

                db.SaveChanges();


            }
            return list;
        }
        public ActionResultAsList<OnSite> LayDSOnSiteHienHanh()
        {
            DateTime dt = DateTime.Today;
            ActionResultAsList<OnSite> ob = new ActionResultAsList<OnSite>();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    var list = db.OnSites.Where(d => d.NgayBD <= dt && d.NgayKT >= dt);
                    ob.ObjectList = list.ToList();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ob.AddException(ex);
                }

            }
            return ob;
        }
        public ActionResultAsList<OnSite> TimKiemDSOSTheoDiaDiemVaCongTy(string congty, string diadiem)
        {

            ActionResultAsList<OnSite> ob = new ActionResultAsList<OnSite>();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    var list = db.OnSites.Where(d => d.CongTy.Contains(congty));
                    ob.ObjectList = list.ToList();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ob.AddException(ex);
                }

            }
            return ob;
        }
        public ActionResultAsList<OnSite> TimKiemDSOSTheoThangVaNam(int thang, int nam)
        {

            ActionResultAsList<OnSite> ob = new ActionResultAsList<OnSite>();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    var list = db.OnSites.Where(d => d.NgayBD.Month == thang && d.NgayKT.Year == nam);
                    ob.ObjectList = list.ToList();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ob.AddException(ex);
                }

            }
            return ob;
        }
        public ActionResultAsList<NhanVien> LayDSNVRROS()
        {
            ActionResultAsList<NhanVien> ob = new ActionResultAsList<NhanVien>();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    ob.ObjectList = db.NhanViens.Where(o => o.TrangThai == TrangThaiNV.RanhRoi || o.TrangThai == TrangThaiNV.OnSite).ToList();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ob.AddException(ex);
                }

            }
            return ob;
        }
    }
}
